/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0); // 创建对象
        ListNode cur = dummy;   // 通用写法-单链表遍历（多个引用，指向同一对象，对象存入堆空间，引用存入栈空间）
        int carry = 0;  // 进位标签
        while (l1 != null || l2 != null || carry != 0) {  // 全为空且进位0
            int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry; // 当前值
            carry = s / 10;  // 进位值
            cur.next = new ListNode(s % 10);  // 保留值
            cur = cur.next;
            l1 = l1 == null ? null : l1.next;  // 三目运算 (a<b)?c:d
            l2 = l2 == null ? null : l2.next;  // l1 l2 遍历
        }
        return dummy.next;
    }
}